package indi.service;
import java.io.File;
import java.util.*;

import indi.entity.Book;
import indi.entity.BorrowBookLiscence;
import indi.entity.Manager;
import indi.entity.RecordOfBorrow;

public class BookSystem {
	
	//当前系统管理员
	private Manager currentSysMng;
	//当前图书管理员
	public static Manager currentBookManager;
	//当前图书管理员
	private ArrayList<Manager> currentManagers;
	//当前系统标题
	private Title currentTitle;
	//当前借阅证管理者
	private LiscenceManager currentLscMng;
	//当前借书记录管理者
	private RecordManager currentRcdMng;
	//当前图书管理者
	private BookManager currentBkMng;
	
	
	//构造函数
	public BookSystem(Manager currentManager) throws Exception{
		super();
		this.currentManagers = new ArrayList<>();
		this.currentBookManager = currentManager;//new Manager(username, pwd);
		this.currentTitle = new Title(null);
		/*
		 * 添加默认数据
		 */
		currentManagers.add(new Manager("guan","121212"));
		currentManagers.add(new Manager("li","232323"));
		//如果系统中没有该图书管理员，则抛出异常
		if(false == currentManagers.contains(currentBookManager)) {
			throw new Exception("No existing manager!");
		}
		this.currentLscMng = new LiscenceManager();
		this.currentRcdMng = new RecordManager();
		this.currentBkMng = new BookManager();
	}
	
	//功能面板
	public static int menu() {
		System.out.println("1. 注册借阅证");//
		System.out.println("7. 删除借阅证");
		System.out.println("2. 添加管理员");//
		System.out.println("8. 删除管理员");
		System.out.println("9. 添加标题");
		System.out.println("3. 添加图书");//
		System.out.println("10.删除图书");
		System.out.println("11.更新图书");
		System.out.println("12.查询图书");//
		System.out.println("4. 借书");//
		System.out.println("5. 还书");//
		System.out.println("6. 查询借阅记录");
		System.out.println("13.显示标题信息");
		System.out.println("其他. 退出");
		System.out.print("输入选项：");
		return new Scanner(System.in).nextInt();
	}
	
	//注册借阅证
	public boolean registeLiscence(String name, String acadmy) {
	
		Scanner s = new Scanner(System.in);
		
		try {
			//System.out.print("输入学生姓名：");
			//String name = s.next();
			//System.out.print("输入学生系别：");
			//String acadmy = s.next();
			
			int id = 0;
			id = currentLscMng.addLiscence(new BorrowBookLiscence(
					new Student(name, acadmy)));
			if( id != 0) {
				System.out.println("注册证号为 " + id);
				return true;
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		return false;
	}
	
	//删除借阅证
	public boolean deleteLiscence() {
		Scanner s = new Scanner(System.in);
		
		try {
			System.out.println("输入借阅证号：");
			int liscenceId = s.nextInt();
			
			return currentLscMng.deleteLiscence(liscenceId);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	//添加管理员
	public boolean addManager() {
		
		Scanner s = new Scanner(System.in);
		
		try {
			System.out.print("输入用户名：");
			String username = s.next();
			System.out.print("输入密码：");
			String password = s.next();
			
			if(true == currentManagers.add(new Manager(username, password))){
				System.out.println("注册管理员成功！");
				return true;
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		System.out.println("注册管理员失败！");
		return false;
	}
	
	//删除管理员
	public boolean deleteMangaer() {
		Scanner s = new Scanner(System.in);
		
		try {
			System.out.print("输入用户名：");
			String username = s.next();
			System.out.print("输入密码：");
			String password = s.next();
			
			//通过(用户名，密码)删除图书管理员
			if(true == currentManagers.remove(new Manager(username, password))) {
				System.out.println("删除图书管理员成功！");
				return true;
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		System.out.println("删除图书管理员失败！");
		return false;
	}
	
	//更新添加标题信息
	public boolean addTitle() {
		Scanner s = new Scanner(System.in);
		
		try {
			System.out.print("输入标题：");
			String title = s.next();
			
			currentTitle.setCurrentContent(title);
			return true;
		}catch(Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/*
	 * 注意：无需实现，只需更新标题即可。
	 * 功能：删除标题信息
	 */
	public boolean deleteTitle() {
		return false;
	}
	
	//添加图书
	public boolean addBook() {
		Scanner s = new Scanner(System.in);
		
		try {
			System.out.print("输入作者：");
			String author = s.next();
			System.out.print("输入书名：");
			String bookName = s.next();
			System.out.print("输入类型：");
			String genre = s.next();
			System.out.print("输入ISBN：");
			String isbn = s.next();
						
			if(true == currentBkMng.addOneBook(bookName,author,genre,isbn)) {
				System.out.println("添加图书成功！");
				return true;
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		System.out.println("添加图书失败！");
		return false;
	}
	
	//删除图书
	public boolean deleteBook() {
		Scanner s = new Scanner(System.in);
		
		try {
			System.out.print("输入ISBN：");
			String isbn = s.next();
						
			if(true == currentBkMng.deleteOneBook(isbn)) {
				System.out.println("删除图书成功！");
				return true;
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		System.out.println("删除图书失败！");
		return false;
	}
	
	//更新图书
	public boolean updateBook() {
		Scanner s = new Scanner(System.in);
		
		try {
			//输入ISBN，通过ISBN查找图书
			System.out.print("输入ISBN：");
			String isbn = s.next();
			Book bookToQuery = currentBkMng.queryBookByIsbn(isbn);
			//为防止中断系统导致的更细中断，不采用直接更新书本数据的方式，利用BookManager更新书本
			System.out.print("输入新作者(无需更改，请输入1)：");
			String newAuthor = s.next();
			if(!newAuthor.equals("1")) {
				newAuthor = bookToQuery.getAuthor();
			}
			System.out.print("输入新书名：");
			String newBookName = s.next();
			if(newBookName.equals("1")) {
				newBookName = bookToQuery.getBookName();
			}
			System.out.print("输入新类型：");
			String newGenre = s.next();
			if(newGenre.equals("1")) {
				newGenre = bookToQuery.getGenre();
			}
			System.out.print("输入新ISBN：");
			String newIsbn = s.next();
			if(newIsbn.equals("1")) {
				newIsbn = bookToQuery.getIsbn();
			}
			
			if(true == currentBkMng.updateOneBook(bookToQuery, newBookName,newAuthor,newGenre,newIsbn)) {
				System.out.println("更新图书成功！");
				return true;
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		System.out.println("更新图书失败！");
		return false;
	}
	
	//查询图书
	public ArrayList<Book> queryBooks() {
		Scanner s = new Scanner(System.in);
		
		try {
			System.out.print("输入书名：");
			String bookName = s.next();
						
			ArrayList<Book> books = currentBkMng.queryBookByBookName(bookName);
			//打印图书
			for(int i= 0; i<books.size(); i++) {
				books.get(i).printBook();
			}
			System.out.println();
			return books;
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	//借书
	// 接口1public boolean borrowBook() {
	public boolean borrowBook(int liscenceId, String isbn) {	
		Scanner s = new Scanner(System.in);
		
		try{
		
			//System.out.print("输入借阅证号：");
			//int liscenceId = s.nextInt();// 不需，改为参数
			
			//查找借阅证
			BorrowBookLiscence liscenceToQuery = 
					currentLscMng.queryLiscence(liscenceId);
			
			//查找到该借阅证
			if(null == liscenceToQuery) {
				System.out.println("没有找到该借阅证号！");
				return false;
			}
			//验证有逾期记录
			if(true == currentLscMng.haveOverdueRecord(liscenceToQuery)) {
				System.out.println("借书证存在逾期记录！");
				return false;
			}
			//验证借阅记录达到上限
			if(true == currentLscMng.isReachUpperLimit(liscenceToQuery)) {
				System.out.println("借书证借书达上限！");
				return false;
			}
			
			//System.out.print("输入ISBN：");//改为参数，所以注释掉
			//s.nextLine();// next()缺陷，需要消掉
			//String isbn = s.next();
			//通过isbn找到书本
			Book bookToQuery = currentBkMng.queryBookByIsbn(isbn);
				
			//没有查找到书本
			if(null == bookToQuery) {
				System.out.println("没有找到该书本！");
				return false;
			}
			//图书不在馆
			if(false == currentBkMng.isInLibrary(bookToQuery)) {
				System.out.println("该书已借出！");
				return false;	
			}
			
			//想系统中添加借书记录
			RecordOfBorrow newRecord = currentRcdMng.addBorrowRecord(liscenceToQuery, bookToQuery.getIsbn(), new Date());
			//添加书本与借书记录的联系，及对书本的修改
			currentBkMng.addRelatedRecord(bookToQuery, newRecord);
			//添加借阅证与记录的联系
			currentLscMng.addRelatedBook(liscenceToQuery, newRecord);
			
			System.out.println("借书成功！");
			return true;				
			
		}catch(Exception e) {
			e.printStackTrace();
		}		
		
		return false;
	}
	//还书
	public boolean returnBook(String isbn) {
		Scanner s = new Scanner(System.in);
		
		try {
			//System.out.print("输入ISBN：");//改为参数
			//String isbn = s.next();
			
			//通过isbn查找书本
			Book book = currentBkMng.queryBookByIsbn(isbn);
			//判断isbn是否在馆
			if(null == book) {
				System.out.println("本馆中没有该图书！");
				return false;
			}
			//通过isbn借书记录
			RecordOfBorrow recordToQuery = currentRcdMng.queryBorrowRecord(isbn);
			//判断本馆是否存在该借书记录
			if(null == recordToQuery) {
				System.out.println("本馆没有该书本的借书记录！");
				return false;
			}
			
			//获取借书记录关联的借阅证
			BorrowBookLiscence liscence = recordToQuery.getCurrentLisence();
			//判断是否查找到借阅证
			if(null == liscence) {
				System.out.println("借书记录没有关联借阅证！");
				return false;
			}
			//判断借阅证是否有逾期记录
			if(true == currentLscMng.haveOverdueRecord(liscence)) {
				System.out.println("借书证有逾期记录，请给予警告！");
				System.out.print("是否继续还书(1是/其他否)：");
				if(s.nextInt() != 1) {
					System.out.println("还书失败！");
					return false;
				}
			}
			
			//取消书本与借阅记录之间联系
			currentBkMng.deRelatedRecord(book);
			//删除借阅证关联的借阅记录
			currentLscMng.deRelatedRecord(liscence, recordToQuery);
			//删除系统中的借阅记录
			currentRcdMng.deleteBorrowRecord(isbn);
			
			System.out.println("还书成功！");
			return true;
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		return false;
	}
	//查询借阅记录，输出借阅记录信息
	public 
	//ArrayList<RecordOfBorrow> 
	void queryRecords(int liscenceId){
		Scanner s = new Scanner(System.in);
		
		try {
			//System.out.println("输入借书证号：");//改为参数
			//int liscenceId = s.nextInt();
			//获取借阅证
			BorrowBookLiscence liscence = currentLscMng.queryLiscence(liscenceId);
			//判断是否存在该借阅证
			if(null == liscence) {
				System.out.println("系统中没有该借阅证！");
				//return null;
				return ;
			}
			//获取借阅证的借阅记录，不通过借阅证管理类
			ArrayList<RecordOfBorrow> records = liscence.getCurrentRecords();
			//输出借阅记录信息
			for(RecordOfBorrow e:records) {
				System.out.println(e.getCurrentLisence().toString() + " , " + 
						e.getCurrentBookIsbn().toString() + " , " + 
						e.getLastBorrowedTime().toGMTString()  + " , " + 
						e.getReturnTime().toGMTString());
			}
			
			//return records;
		}catch(Exception e) {
			e.printStackTrace();
		}
		//return null;
	}
	//维护系统
	public void RepairSystem() {
		
	}
	//打印标题
	public void showTitle() {
		System.out.println(currentTitle.getCurrentContent());
	}
}